package com.ylxn.three;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.MessageProperties;
import com.ylxn.utils.RabbitMqUtils;

import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * @author cenghaile
 * @version 1.0
 * 2021/7/1 11:54 上午
 * 消息手动应答保证消息不丢失
 **/
public class Task02 {
    public static final String QUEUE_NAME = "ack_queue";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        channel.confirmSelect();
        /**
         * 申明队列
         */
        channel.queueDeclare(QUEUE_NAME,false,false,false,null);
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            String next = scanner.next();

            //设置生产者发送消息为持久化消息（要保存在磁盘中）
            channel.basicPublish("",QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN ,next.getBytes(StandardCharsets.UTF_8));
            System.out.println("生产者发送消息" + next);
        }


    }
}
